<?php
session_start();

// 检查用户是否已登录
if (!isset($_SESSION['username'])) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => '未登录']);
    exit();
}

// 获取POST数据
$data = json_decode(file_get_contents('php://input'), true);

if (!$data) {
    header('Content-Type: application/json');
    echo json_encode(['success' => false, 'message' => '无效的请求数据']);
    exit();
}

try {
    // 尝试连接数据库
    $dsn = "mysql:host={$data['address']};dbname={$data['db_name']}";
    $pdo = new PDO($dsn, $data['username'], $data['password'], [
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_TIMEOUT => 5 // 设置连接超时时间为5秒
    ]);
    
    // 测试查询
    $pdo->query('SELECT 1');
    
    header('Content-Type: application/json');
    echo json_encode(['success' => true]);
} catch (PDOException $e) {
    header('Content-Type: application/json');
    echo json_encode([
        'success' => false,
        'message' => $e->getMessage()
    ]);
}
?> 